//
//  VCPlane.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/19.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCPlane: VCBase {
    
    // 头部背景图片
    private lazy var topImg: UIImageView = {
        let ui = UIImageView(image: UIImage(named: "background_plane"))
        ui.contentMode = .scaleToFill
        ui.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT * 0.3)
        return ui
    }()
    
    // 切换按钮
    private lazy var segmentView: SegmentView = {
        let ui = SegmentView()
        ui.delegate = self
        let uiY = topImg.frame.maxY - MARGIN50
        ui.frame = CGRect(x: MARGIN, y: uiY, width: SCREEN_WIDTH - MARGIN * 2, height: SCREEN_HEIGHT * 0.7)
        return ui
    }()
    
    private var scrollview = UIScrollView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_views()
        init_setting()
    }
}

extension VCPlane: UIScrollViewDelegate {
    
    func init_views() -> Void {
        scrollview.addSubview(topImg)
        scrollview.addSubview(segmentView)
        view.addSubview(scrollview)
    }
    
    func init_setting() -> Void {
        scrollview.delegate = self
        scrollview.showsVerticalScrollIndicator = false
        scrollview.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        scrollview.contentSize = CGSize(width: SCREEN_WIDTH, height: SCREEN_HEIGHT + MARGIN20)
        scrollview.contentInset = UIEdgeInsets(top: -realHeight, left: 0, bottom: 0, right: 0)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        scrollView.endEditing(true)
    }
}

// 代理实现
extension VCPlane: SegmentViewDelegate {
    
    // 点击搜索按钮
    func segment_search(dict: NSDictionary,index: Int) {
        view.endEditing(true)
        // 校验
        let flightnumberid = dict.value(forKey: "flightnumberid") as? String
        if index == 0 ,flightnumberid?.count == 0 {
            self.show(text: "航班号不能为空")
            return
        }
        let vc = VCShowPlay()
        vc.requestDict = dict
        vc.title = dict.value(forKey: "departdateall") as? String
        self.pushNextVcHideShowBottomBar(vc: vc)
    }
    
    // 到达城市点击
    func segment_stopCity() {
        let vc = VCCitySelect()
        vc.title = "选择到达城市"
        vc.complete = {city in
            self.segmentView.resetStopCity = city
        }
        self.pushNextVcHideShowBottomBar(vc: vc)
    }
    
    // 出发城市点击
    func segment_startCity() {
        let vc = VCCitySelect()
        vc.title = "选择出发城市"
        vc.complete = {city in
            self.segmentView.resetStartCity = city
        }
        self.pushNextVcHideShowBottomBar(vc: vc)
    }
    
    // 日期选择
    func segment_selectDate() {
        let vc = VCDateSelect()
        vc.title = "选择出发日期"
        vc.complete = {date in
            self.segmentView.resetStartDate = date
        }
        self.pushNextVcHideShowBottomBar(vc: vc)
    }
}
